/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.web.controller;

import cc.altius.model.*;
import cc.altius.service.AmbulanceSerService;
import cc.altius.service.AmbulanceService;
import cc.altius.service.CenterService;
import cc.altius.service.CityService;
import cc.altius.service.HospitalService;
import cc.altius.service.LocumService;
import cc.altius.service.ZoneService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

/**
 *
 * @author sagar
 */
@Controller
public class HospitalController {

    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private ZoneService zoneService;
    @Autowired
    private AmbulanceSerService ambulanceSerService;
    @Autowired
    private CenterService centerService;
    @Autowired
    private LocumService locumService;
    @Autowired
    private CityService cityService;
    @Autowired
    private AmbulanceService ambulanceService;

//    @ModelAttribute("hospitalList")
//    List<Hospital> getHospitalList() {
//        return this.hospitalService.getHospitalList();
//    }
//    
    @ModelAttribute("zoneList")
    List<Zone> getZoneList() {
        return this.hospitalService.getZoneList();
    }

    @ModelAttribute("masterZoneList")
    public List<MasterZone> populateMasterZoneList() {
        return this.zoneService.getMasterZoneList();
    }

    @ModelAttribute("cityList")
    public List<City> populateCityList() {

        return this.zoneService.getCityList();

    }

    @ModelAttribute("stateList")
    public List<State> populateStateList() {
        List<State> stateList = this.cityService.getStateList();
        return stateList;
    }

    @ModelAttribute("centerList")
    public List<HealthSpringCenter> getCenterList() {
        List<HealthSpringCenter> center = this.hospitalService.getCenterList();
        return center;
    }

    @ModelAttribute("ambulanceServiceList")
    List<AmbulanceSer> getAmbulanceServiceList() {
        return this.ambulanceSerService.getAmbulanceServiceList();
    }

    @RequestMapping(value = "/hospital/addHospital.htm", method = RequestMethod.GET)
    public String showHospitalPage(ModelMap modelMap) {
        Hospital hospital = new Hospital();
//        Zone zone= new Zone();
//        hospital.setZoneId(zone);
        modelMap.addAttribute("hospital", hospital);

        return "hospital/addHospital";
    }

    @RequestMapping(value = "/hospital/addHospital.htm", method = RequestMethod.POST)
    public String addHospitalPage(@ModelAttribute("hospital") Hospital hospital, Errors errors, SessionStatus sessionStatus) {

        int hospitalId = this.hospitalService.addHospital(hospital);
        if (hospitalId == 0) {
            errors.rejectValue("hospitalName", "msg.hospitalError");
            return "hospital/addHospital";
        } else {
            sessionStatus.setComplete();
            return "redirect:addHospital.htm?msg=Succesfully Added Hospital";
        }
    }

    @RequestMapping(value = "/hospital/listHospital.htm", method = RequestMethod.GET)
    public String showHospitalListPage(ModelMap modelMap) {
        List<Map<String, Object>> filterHospital = this.hospitalService.getFilterHospital(0, 0, 0, 0,0);
        modelMap.addAttribute("filterHospital", filterHospital);
        modelMap.addAttribute("cityId", 0);
        modelMap.addAttribute("stateId", 0);
        modelMap.addAttribute("masterZoneId", 0);
        modelMap.addAttribute("zoneId", 0);
        modelMap.addAttribute("tieUpId", 0);
        return "/hospital/listHospital";
    }

    @RequestMapping(value = "/hospital/listHospital.htm", method = RequestMethod.POST)
    public String filterOnHospital(@RequestParam(value = "stateId", required = true, defaultValue = "0") int stateId,
            @RequestParam(value = "cityId", required = true, defaultValue = "0") int cityId,
            @RequestParam(value = "masterZoneId", required = true, defaultValue = "0") int masterZoneId,
            @RequestParam(value = "zoneId", required = true, defaultValue = "0") int zoneId,
            @RequestParam(value = "tieUpId", required = true, defaultValue = "0") int tieUpId,
            ModelMap modelMap) {

        List<Map<String, Object>> filterHospital = this.hospitalService.getFilterHospital(stateId, cityId, masterZoneId, zoneId,tieUpId);
        modelMap.addAttribute("filterHospital", filterHospital);
        modelMap.addAttribute("cityId", cityId);
        modelMap.addAttribute("stateId", stateId);
        modelMap.addAttribute("masterZoneId", masterZoneId);
        modelMap.addAttribute("zoneId", zoneId);
        modelMap.addAttribute("tieUpId", tieUpId);

        return "/hospital/listHospital";
    }

    @RequestMapping(value = "/hospital/updateHospital.htm", method = RequestMethod.GET)
    public String showUdateHospitalPage(@RequestParam(value = "hospitalId") int hospitalId, ModelMap modelMap) {

        Hospital hospital = this.hospitalService.getHospitalById(hospitalId);
        modelMap.addAttribute("hospital", hospital);
        modelMap.addAttribute("hospital", hospital);
        //modelMap.addAttribute("zoneList", zoneList);

        return "hospital/updateHospital";
    }

    @RequestMapping(value = "/hospital/updateHospital.htm", method = RequestMethod.POST)
    public String onSubmitPage(@ModelAttribute("hospital") Hospital hospital, @RequestParam(value = "hospitalId", required = true) int hospitalId,
            SessionStatus sessionStatus, HttpServletRequest request) {
        //int campaignTypeId=ServletRequestUtils.getIntParameter(request, "campaignTypeId",0);
        System.out.println("in controller of post hospital");
        this.hospitalService.updateHospital(hospital, hospitalId);
        return "redirect:listHospital.htm?msg=Succesfully Updated Hospital";

    }

    @RequestMapping(value = "/hospital/showHospitals.htm", method = RequestMethod.GET)
    public String getShowHospitalsPage(ModelMap modelMap) {
        List<Hospital> hospitalList = this.hospitalService.getHospitalList();
        modelMap.addAttribute("hospitalList", hospitalList);
        System.out.println("hospitalList :"+hospitalList);
        List<Locum> locumList = this.locumService.getLocumList();
        modelMap.addAttribute("locumList", locumList);
        List<Ambulance> ambulanceList = this.ambulanceService.getAmbulanceList();
        modelMap.addAttribute("ambulanceList", ambulanceList);
        return "hospital/showHospitals";
    }

    @RequestMapping(value = "/hospital/ajaxCallForZoneList.htm", method = RequestMethod.GET)
    public @ResponseBody
    String doAjaxZoneListPopulate(HttpServletRequest request) {
        String str = request.getParameter("term");
        String json = null;
        List<Zone> zoneList = this.zoneService.listZoneForAutocomplete(str);
        Gson gson = new Gson();
        List list = new ArrayList();
        for (Zone z : zoneList) {

            Map<String, Object> map = new HashMap<String, Object>();
            System.out.println("id =" + z.getZoneId());
            map.put("id", z.getZoneId());
            map.put("label", z.getZoneName() + "~" + z.getZoneId());
            map.put("value", z.getZoneName() + "~" + z.getZoneId());
            list.add(map);
        }
        Type typeList = new TypeToken<List>() {
        }.getType();
        json = gson.toJson(list, typeList);
        System.out.println("json= " + json);
        return json;
    }

    @RequestMapping(value = "/hospital/showHospitals.htm", method = RequestMethod.POST)
    public String postShowHospitalsPage(@ModelAttribute("hospital") Hospital hospital, HttpServletRequest request, ModelMap modelMap) throws ServletRequestBindingException {
        String zoneDetails = ServletRequestUtils.getStringParameter(request, "zoneName");
        System.out.println("zone details " + zoneDetails);
        String temp[] = zoneDetails.split("~");



        return "hospital/showHospitals";
    }

    @RequestMapping(value = "/hospital/searchResultOnZipCode.htm", method = RequestMethod.GET)
    public String showsearchOnZipCodePage() {
//        ZipCode zip = new ZipCode();
//        modelMap.addAttribute("zip", zip);
        return "hospital/searchResultOnZipCode";
    }

    @RequestMapping(value = "/hospital/searchResultOnZipCode.htm", method = RequestMethod.POST)
    public String PostshowsearchOnZipCodePage(@RequestParam("zipCode") String zipCode, ModelMap modelMap) {
        System.out.println("inside post");
        modelMap.addAttribute("zipCode", zipCode);
        System.out.println("zipCode" + zipCode);
        List<Map<String, Object>> list = this.hospitalService.getListOnZipCode(zipCode);
        if (!list.isEmpty()) {
            System.out.println("list : " + list);

            modelMap.addAttribute("list", list);
            return "hospital/searchResultOnZipCode";
        } else {
            return "redirect:/hospital/searchResultOnZipCode.htm?error=No Record Found For " + zipCode + "";
        }
    }

    @RequestMapping(value = "/hospital/showHospitalDetails.htm", method = RequestMethod.GET)
    public String showHospitalDetails(@RequestParam("hospitalId") String hospitalId, ModelMap modelMap) {
        Hospital hospital = this.hospitalService.getHospitalById(Integer.parseInt(hospitalId));
        modelMap.addAttribute("hospital", hospital);
        return "hospital/showHospitalDetails";
    }

    @RequestMapping(value = "/hospital/addZipCode.htm", method = RequestMethod.GET)
    public String showZipCodePage(ModelMap modelMap) {
        ZipCodeArea zipCodeArea = new ZipCodeArea();
        modelMap.addAttribute("zipCodeArea", zipCodeArea);
        return "hospital/addZipCode";
    }

    @RequestMapping(value = "/hospital/addZipCode.htm", method = RequestMethod.POST)
    public String postZipCodePage(@ModelAttribute("zipCodeArea") ZipCodeArea zipCodeArea, Errors errors, HttpServletRequest request,
            SessionStatus sessionStatus) {

//        if (this.hospitalService.existZipCodeByZipCode(zipCode.getZipCode())) {
//            
//            errors.rejectValue("zipCode", "msg.duplicateZipCode");
//            return "/hospital/addZipCode";
//        }

        int zipCodeId = this.hospitalService.addZip(zipCodeArea);
        if (zipCodeId == 0) {
            errors.rejectValue("zipCode", "msg.zipCodeError");
            return "/hospital/addZipCode";
        } else {
            sessionStatus.setComplete();
            return "redirect:listZipCode.htm?msg=Succesfully added zipcode";
        }

    }

    @RequestMapping(value = "/hospital/listZipCode.htm", method = RequestMethod.GET)
    public String showZipCodeListPage(ModelMap modelMap) {
        List<ZipCodeArea> zipCodeList = this.hospitalService.getzipCodeList();
        modelMap.addAttribute("zipCodeList", zipCodeList);

        return "/hospital/listZipCode";
    }

    @RequestMapping(value = "/hospital/editZipCode.htm", method = RequestMethod.GET)
    public String showEditZipCodePage(@RequestParam(value = "zipCodeId") int zipCodeId, ModelMap modelMap) {

        ZipCodeArea zipCodeArea = this.hospitalService.getZipCodeById(zipCodeId);
        modelMap.addAttribute("zipCodeArea", zipCodeArea);

        return "hospital/editZipCode";
    }

    @RequestMapping(value = "/hospital/editZipCode.htm", method = RequestMethod.POST)
    public String editZipCodePostPage(@ModelAttribute("zipCodeArea") ZipCodeArea zipCodeArea, @RequestParam(value = "zipCodeId", required = true) int zipCodeId,
            SessionStatus sessionStatus, HttpServletRequest request) {
        //int campaignTypeId=ServletRequestUtils.getIntParameter(request, "campaignTypeId",0);
        this.hospitalService.updateZip(zipCodeArea, zipCodeId);
        return "redirect:listZipCode.htm?msg=Succesfully Updated ZipCode";

    }

    @RequestMapping(value = "/hospital/getCenterCheckBox.htm", method = RequestMethod.GET)
    public @ResponseBody
    String doAjaxCenterrPopulate(HttpServletRequest request) {
        List<HealthSpringCenter> centerList = this.hospitalService.getCenterList();
        Gson gson = new Gson();
        Type typeList = new TypeToken<List>() {
        }.getType();
        String json = gson.toJson(centerList, typeList);
        return json.toString();
    }
}
